package com.wolfking.jeesite.condition;

import com.google.common.collect.Sets;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Set;

@Order(Ordered.LOWEST_PRECEDENCE - 20)
class OnSpringApplicationNameCondition extends SpringBootCondition {

    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String[] env = (String[]) metadata.getAnnotationAttributes(ConditionalOnSpringApplicationName.class.getName())
                .get("value");
        Set<String> nameSet = Sets.newHashSet(env);
        String applicationName = context.getEnvironment().getProperty("spring.application.name");
        boolean result = nameSet.contains(applicationName);
        return new ConditionOutcome(result, ConditionMessage
                .forCondition(ConditionalOnSpringApplicationName.class, "expression").resultedIn(result));
    }
}
